package com.wwl.io;

import java.io.*;

public class ObjectInputStreamDemo {
    public static void main(String[] args) {
        ObjectInputStreamDemo.write();
        ObjectInputStreamDemo.read();
    }

    public static void write(){
        Person p =  new Person("Tom",20);
        Person p1 =  new Person("MIke",18);
        try {
            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("data.pta"));
            oos.writeObject(p);
            oos.writeObject(p1);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void read(){
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.pta"));
            Person p = (Person) ois.readObject();
            while(p!=null){
                System.out.println("name="+p.getName()+",age="+p.getAge());
                if(ois.available()!=-1){
                    p= (Person) ois.readObject();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


}
